/*
* SuctionCalController.java
* Created on  2015/7/21 10:29
* 版本       修改时间          作者      修改内容
* V1.0.1    2015/7/21       longshihui    初始版本
*
*/

package com.infitecs.eyas.oncefan;

import com.infitecs.eyas.expression.service.ExpressionService;
import com.infitecs.eyas.expression.service.ToolExpressionService;
import com.infitecs.eyas.oncefan.model.OnceFanBaseCal;
import com.infitecs.eyas.oncefan.model.OnceFanCalculator;
import com.infitecs.eyas.oncefan.service.OnceFanCalculatorService;
import com.infitecs.eyas.session.SessionService;
import com.infitecs.eyas.session.model.Param;
import com.infitecs.eyas.suction.model.SuctionCalculate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 类的描述信息
 *
 * @author longshihui
 * @version 1.0.1
 */
@Controller
@RequestMapping("/OnceFan/calculate")
public class OnceCalController {

    @Autowired
    private SessionService sessionService;

    @Autowired
    private OnceFanCalculatorService onceFanCalculatorService;

    @Autowired
    private ToolExpressionService toolExpressionService;

    @RequestMapping("")
    public String getPage(Model model) {
        try {
            sessionService.getCaseId();
            List<OnceFanCalculator> onceFanCalculators = onceFanCalculatorService.getOnceFanCalculatorList();
            if (onceFanCalculators == null || onceFanCalculators.isEmpty()) {
                onceFanCalculatorService.insertOnceFanCalculator();
            }
            OnceFanBaseCal onceFanBaseCal = onceFanCalculatorService.getOnceFanBaseCal();
            if (onceFanBaseCal == null){
                onceFanCalculatorService.insertNewOnceFanBase();
                onceFanBaseCal = onceFanCalculatorService.getOnceFanBaseCal();
            }
            List<OnceFanCalculator> in = onceFanCalculatorService.getIn();
            List<OnceFanCalculator> out = onceFanCalculatorService.getOut();
            model.addAttribute("in", in);
            model.addAttribute("out", out);
            model.addAttribute("onceFanBaseCal",onceFanBaseCal);
            sessionService.setEnvironment('C');
        } catch (Exception e) {
            return "oncefan/calculate";
        }
        return "oncefan/calculate";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public int add(OnceFanCalculator onceFanCalculator) {
        return onceFanCalculatorService.insertOneOnceFan(onceFanCalculator);
    }

    @RequestMapping(value = "/del", method = RequestMethod.DELETE)
    @ResponseBody
    public void del(@RequestParam("id") int id) {
        onceFanCalculatorService.del(id);
    }

    @RequestMapping(value = "/editTitle", method = RequestMethod.POST)
    @ResponseBody
    public void editTitle(OnceFanCalculator onceFanCalculator) {
        onceFanCalculatorService.editTitle(onceFanCalculator);
    }

    //以下代码可以复用
    @RequestMapping(value = "/setE", method = RequestMethod.GET)
    @ResponseBody
    public void setEv() {
        sessionService.setEnvironment('C');
    }

    @RequestMapping(value = "/getToolBarValues", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Double> getToolBarValues(@RequestParam("id") int id) {
        sessionService.setEnvironment('C');
        return toolExpressionService.getResult();
    }

    @RequestMapping(value = "/autoCal", method = RequestMethod.POST)
    @ResponseBody
    public Double autoCal(OnceFanCalculator onceFanCalculator) {
        return onceFanCalculator.getP() * onceFanCalculator.getZ();
    }

    @RequestMapping(value = "/autoCalSum", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> autoCalSum(@RequestBody List<OnceFanCalculator> onceFanCalculators) {
        //E17没获得
        Double E17 = sessionService.getParamMap().get("CE19");
        double calculateLost = 0;
        double designLost = 0;
        double t;
        Iterator<OnceFanCalculator> i = onceFanCalculators.iterator();
        OnceFanCalculator o;
        while (i.hasNext()) {
            o = i.next();
            t = o.getLost();
            calculateLost += t;
            if (o.isCheck2()) {
                if (E17 != null) {
                    t *= (1 + E17 / 100);
                }else {
                    return null;
                }
            }
            designLost += t;
        }
        Map<String, String> result = new HashMap<>();
        result.put("calculateLost", Double.toString(calculateLost));
        result.put("designLost", Double.toString(designLost));
        this.updateOnceFanRowSum(onceFanCalculators.get(0), calculateLost, designLost);
        return result;
    }

    @RequestMapping(value = "/saveRow", method = RequestMethod.POST)
    @ResponseBody
    public void saveRow(OnceFanCalculator onceFanCalculator) {
        onceFanCalculatorService.update(onceFanCalculator);
    }

    @RequestMapping(value = "/autoCalTableTotal", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> updateTotal(@RequestBody Map<String, Object> wrapper) {
        Double E25 = sessionService.getParamMap().get("CE25");
        Double E32 = sessionService.getParamMap().get("CE32");
        if (E25 == null || E32 == null){
            return null;
        }
        int source = Integer.parseInt(wrapper.get("source").toString());
        List<Map<String, String>> dataWrapper = (List<Map<String, String>>) wrapper.get("data");
        double calculateLostTotal = 0;
        double designLostTotal = 0;
        Map<String, String> data;
        Iterator<Map<String, String>> i = dataWrapper.iterator();
        while (i.hasNext()) {
            data = i.next();
            calculateLostTotal += Double.parseDouble(data.get("calculateLost"));
            designLostTotal += Double.parseDouble(data.get("designLost"));
        }
        Map<String, String> result = new HashMap<>();


        if (1 == source) {
            result.put("E34", Double.toString(calculateLostTotal + E25));
            this.updateOnceFanCalculator("E34", calculateLostTotal + E25);
            result.put("E37", Double.toString(designLostTotal + E25));
            this.updateOnceFanCalculator("E37", designLostTotal + E25);
        } else {
            result.put("E35", Double.toString(calculateLostTotal + E32));
            this.updateOnceFanCalculator("E35", calculateLostTotal + E32);
            result.put("E38", Double.toString(designLostTotal + E32));
            this.updateOnceFanCalculator("E38", designLostTotal + E32);
        }
        return result;
    }

    @RequestMapping(value = "/updateParam", method = RequestMethod.POST)
    @ResponseBody
    public void updateOnceFanCalculator(@RequestParam String paramName, @RequestParam Double paramValue) {
        OnceFanBaseCal onceFanBaseCal = new OnceFanBaseCal();
        onceFanBaseCal.set(paramName, paramValue);
        onceFanBaseCal.setCaseId(sessionService.getCaseId());
        sessionService.putParamMap(new Param(sessionService.getE() + paramName,paramValue));
        onceFanCalculatorService.updateParam(onceFanBaseCal);
    }

    private void updateOnceFanRowSum(OnceFanCalculator onceFanCalculator, double RowCalculateLostSum, double RowDesignLostSum) {
        StringBuilder paramName = new StringBuilder();
        switch (onceFanCalculator.getSmallType()) {
            case 1:
                paramName.append("pipe_");
                break;
            case 2:
                paramName.append("parts_");
                break;
            case 3:
                paramName.append("spare_");
                break;
            case 4:
                paramName.append("device_");
                break;
            case 5:
                paramName.append("other_");
                break;
            default:
                break;
        }
        if (onceFanCalculator.getLagreType() == 1) {
            paramName.append("in_");
        } else {
            paramName.append("out_");
        }
        this.updateOnceFanCalculator(paramName.toString() + "one", RowCalculateLostSum);
        this.updateOnceFanCalculator(paramName.toString() + "two", RowDesignLostSum);
    }

    @RequestMapping(value = "/getCalculateInit", method = RequestMethod.POST)
    @ResponseBody
    public void getCalculateInit() {
        sessionService.putOnceFanCalInit(sessionService.getCaseId());
    }

}
